import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:local_task/controller/task_controller.dart';
import 'package:local_task/model/task_model.dart';
import 'package:local_task/pages/task_list.dart';

/// 任务
class Task extends StatelessWidget {
  Task({Key? key}) : super(key: key);

  final TaskController taskController = Get.put(TaskController());

  /// 默认任务分组。
  final List<AddTaskOptions> _defaultTaskGroup = [
    AddTaskOptions(icon: Icons.sunny, taskType: TaskType.today),
    AddTaskOptions(icon: Icons.task, taskType: TaskType.task)
  ];



  @override
  Widget build(BuildContext context) {
    _initTask();

    return Obx(() {
      return ListView(
        // 构建任务分组列表。
        children: _defaultTaskGroup.map((e) {
          return ListTile(
            leading: Icon(e.icon),
            title: Text(e.taskType!.desc),
            trailing: e.taskType == TaskType.today
                ? Text("${taskController.todayTaskNum}")
                : Text("${taskController.taskNum}"),

            onTap: () => {
              Get.to(TaskList(
                title: e.taskType!.desc,
              ))
            },
          );
        }).toList(),
      );
    });
  }

  /// 初始化：
  ///   1. 【我的一天】数量。
  ///   2. 【任务】数量。
  void _initTask() {
    int todayTaskCount = taskController.todayTaskCount();
    taskController.todayTaskNum.value = todayTaskCount;

    int taskCount = taskController.taskCount();
    taskController.taskNum.value = taskCount;

  }
}
